import 'package:flutter/material.dart';
import 'package:wheel_of_fortune/http/wheel_server.dart';
import 'package:wheel_of_fortune/model/rotary_table_model.dart';
import 'package:wheel_of_fortune/utils/utils.dart';
import 'package:wheel_of_fortune/widgets/message_alerts.dart';

class NothingDialog extends StatelessWidget {
  const NothingDialog({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        alignment: Alignment.center,
        child: Stack(
          children: <Widget>[
            Container(
              child: Image.asset('assets/images/fortune_wheel_lose.png',),
            ),
            Positioned(
              left: 10,
              right: 10,
              bottom: 100,
              child: Text("Sorry, you got nothing.\nPlease try again.",
                textAlign: TextAlign.center,
                style: TextStyle(color: Color(0xFF041D57), fontSize: 16,fontFamily: 'Avenir Next LT Pro',fontWeight: FontWeight.w400),
              ),
            ),
            Positioned(
              left: 60,
              right: 60,
              bottom: 10,
              child: InkWell(
                onTap: ()=>Navigator.of(context).pop(false),
                child: Container(
                  height: 35,
                  width: 160,
                  margin: EdgeInsets.only(bottom: 40),
                  decoration: BoxDecoration(
                    color: Color(0xFFFFB32C),
                    borderRadius: BorderRadius.circular(17),
                  ),
                  child: Center(
                    child: Text(
                      'Close',
                      style: TextStyle(
                        color: Color(0xFFFFFFFF),
                        fontSize: 14,
                        fontFamily: 'Avenir Next LT Pro',
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class GetPrizeDialog extends StatefulWidget {
  final PrizeInfos prizeInfos;
  final String id;
  GetPrizeDialog(this.prizeInfos,this.id,{Key key}) : super(key: key);

  @override
  _GetPrizeDialogState createState() => _GetPrizeDialogState();
}

class _GetPrizeDialogState extends State<GetPrizeDialog> {
  bool close = false;
  Widget closeWidget(){
    if (widget.prizeInfos.type == 'ENTITY') {
      return EntityAlert(
        cancel: (){
          setState(() {
            close = false;
          });
        },
      );
    } else {
      return GiftAlert(
        cancel: (){
          setState(() {
            close = false;
          });
        },
        close: ()async{
          try {
            await WheelService().collectPrize(widget.id, null, null, null, null, null);
            showToast(msg: 'You have given up collecting your gift.');
          } catch (e) {
            print(e);
          }
          Navigator.of(context).pop(false);
        },
      );
    }
  }
  @override
  Widget build(BuildContext context) {
    return close?
    closeWidget()
    :GestureDetector(
      child: Container(
        alignment: Alignment.center,
        child: Stack(
          children: <Widget>[
            Container(
              child: Image.asset('assets/images/fortune_wheel_blank.png',),
            ),
            Positioned(
              top: 90,
              left: 36,
              right: 36,
              child: Image.network(widget.prizeInfos.imageUrl,height: 200,fit: BoxFit.cover,),
            ),
            Positioned(
              left: 10,
              right: 10,
              bottom: 120,
              child: Text("Congratulations!",
                textAlign: TextAlign.center,
                style: TextStyle(color: Color(0xFF041D57), fontSize: 22,fontFamily: 'Avenir Next LT Pro',fontWeight: FontWeight.bold),
              ),
            ),
            Positioned(
              top: 60,
              right: 20,
              child: GestureDetector(
                onTap: (){
                  setState(() {
                    close = true;
                  });
                },
                child: Icon(Icons.close, color: Colors.red,),
              ),
            ),
            Positioned(
              left: 10,
              right: 10,
              bottom: 94,
              child: Text("You won ${widget.prizeInfos.name}!",
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                textAlign: TextAlign.center,
                style: TextStyle(color: Color(0xFF041D57), fontSize: 18,fontFamily: 'Avenir Next LT Pro',fontWeight: FontWeight.w400),
              ),
            ),
            Positioned(
              left: 60,
              right: 60,
              bottom: 0,
              child: InkWell(
                onTap: ()=>Navigator.of(context).pop(true),
                child: Container(
                  height: 35,
                  width: 160,
                  margin: EdgeInsets.only(bottom: 40),
                  decoration: BoxDecoration(
                    color: Color(0xFFFFB32C),
                    borderRadius: BorderRadius.circular(17),
                  ),
                  child: Center(
                    child: Text(
                      'Collect Gift',
                      style: TextStyle(
                        color: Color(0xFFFFFFFF),
                        fontSize: 14,
                        fontFamily: 'Avenir Next LT Pro',
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
